/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.proyectos.bean;


import com.proyectos.ejb.CurEjbLocal;
import com.proyectos.model.Cursos;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Jose
 */
public class CtrCursos {

    public CtrCursos() {
    }
        @EJB
    private CurEjbLocal ejb;

    private String name;
    private List<Cursos> lista;
    private Cursos activeReg;
    private boolean lnuevo;
    private boolean ladmin;
    private CtrEntorno oentorno;

    public boolean isLadmin() {
        return ladmin;
    }

    public void setLadmin(boolean ladmin) {
        this.ladmin = ladmin;
    }

    public CtrEntorno getOentorno() {
        return oentorno;
    }

    public void setOentorno(CtrEntorno oentorno) {
        this.oentorno = oentorno;
    }

    public List<Cursos> getLista() {
        return lista;
    }

    public void setLista(List<Cursos> lista) {
        this.lista = lista;
    }

    public Cursos getActiveReg() {
        return activeReg;
    }

    public void setActiveReg(Cursos activeReg) {
        this.activeReg = activeReg;
    }

   

    public boolean isLnuevo() {
        return lnuevo;
    }

    public void setLnuevo(boolean lnuevo) {
        this.lnuevo = lnuevo;
    }


    public void nuevo() {
        try {
            this.lnuevo = true;
            this.ladmin=oentorno.esAdmin()&& lnuevo;
            this.activeReg = new Cursos();
            this.activeReg.setNcarrera(this.oentorno.getCarrera());
            Map<String, Object> options = new HashMap<String, Object>();
            options.put("modal", true);
            options.put("draggable", true);
            options.put("resizable", false);
            options.put("contentHeight", 120);
            options.put("contentWidth", 300);
            RequestContext.getCurrentInstance().openDialog("mantCursos", options, null);
        } catch (Exception ex) {
            Logger.getLogger(CtrAlumnos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void edit(BigDecimal id) {
        try {
            this.lnuevo = false;
            this.ladmin=oentorno.esAdmin()&& lnuevo;
            
            this.activeReg = this.ejb.get(id);

            Map<String, Object> options = new HashMap<String, Object>();
            options.put("modal", true);
            options.put("draggable", true);
            options.put("resizable", false);
            options.put("contentHeight", 120);
            options.put("contentWidth", 300);
            RequestContext.getCurrentInstance().openDialog("mantCursos", options, null);
        } catch (Exception ex) {
            Logger.getLogger(CtrAlumnos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void save(ActionEvent event) {
        try {

            if (this.lnuevo) {
                this.ejb.add(activeReg);
            } else {
                this.ejb.save(activeReg);
            }
            this.buscar();
            RequestContext.getCurrentInstance().closeDialog(null);
        } catch (Exception ex) {
            this.addMessage(ex.getMessage());
            //Logger.getLogger(CtrAlumnos.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

   
    public void addMessage(String summary) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR: ", summary);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void buscar() {
       
             TreeMap vparametros = new TreeMap();
             vparametros.put("ncarrera", this.oentorno.getCarrera());
             if (name != null && !name.isEmpty()) {
                vparametros.put("cnombre", "%" + name.toUpperCase() + "%");
                
             } else {
                vparametros.put("cnombre", "%");
             }
             lista = this.ejb.query("Cursos.findByNombre", vparametros);
  
          
          

    }

    public void buscar_todo() {

        this.buscar();

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void delete(ActionEvent event) {
        try {

            this.ejb.delete(activeReg.getNcodigo());
        } catch (Exception ex) {

            Logger.getLogger(CtrAlumnos.class.getName()).log(Level.SEVERE, null, ex);
        }
        RequestContext.getCurrentInstance().closeDialog(null);
        this.buscar();
    }

    
}
